<template>
    <!-- packageMy/search/search.wxml -->
    <!-- pages/search/search.wxml -->

    <view class="search common-padding">
        <view class="search-btn">
            <van-search
                v-model="searchName"
                shape="round"
                background="transparent"
                placeholder="请输入关键字进行搜索"
                placeholder-style="font-size: 30rpx;color: #999;"
                left-icon=""
                right-icon=""
                @change="onChange"
                @search="onSearch"
                :use-right-icon-slot="true"
            >
                <van-icon slot="right-icon" name="search" @tap.native="onSearch" />
            </van-search>
        </view>
        <block v-if="serchList.length == 0">
            <view class="block-content owner-block" v-for="(item, index) in allList" :key="item.type">
                <view class="orange-block"></view>

                <view class="min-title">
                    <text>{{ item.name }}</text>
                </view>

                <view class="grid-4">
                    <view class="grid-item-4 getMenuData" :data-url="j.funcUrl" :data-type="j.urlType" @tap="goPage" v-for="(j, index1) in item.funtions" :key="j.id">
                        <image :src="filters.replaceImgUrl(j.functionPic)" mode="widthFix"></image>

                        <view>{{ j.funcName }}</view>
                    </view>
                </view>
            </view>
        </block>
        <view class="grid-4" v-else-if="serchList.length > 0">
            <view class="grid-item-4 getMenuData" :data-url="j.funcUrl" :data-type="j.urlType" @tap="goPage" v-for="(j, index) in serchList" :key="j.id">
                <image :src="filters.replaceImgUrl(j.functionPic)" mode="widthFix"></image>

                <view>{{ j.funcName }}</view>
            </view>
        </view>
        <noData v-if="allList.length == 0 && serchList.length == 0" />
    </view>
</template>
<script module="filters" lang="wxs" src="../../utils/filter.wxs"></script>
<script>
// packageMy/search/search.js
const app = getApp();

const api = require('../../utils/api.js');
const { queryAllFuncitonTree, getMenuByUser, getMenuByStaff } =require('../../utils/api.js');

export default {
    data() {
        return {
            mockMenuList: [
                {
                    name: '和谐邻里',
                    type: 7,
                    funtions: [
                        {
                            funcName: '社区圈子',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-1.png'
                        },
                        {
                            funcName: '闲置交易',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-2.png'
                        },
                        {
                            funcName: '邻里互助',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-3.png'
                        },
                        {
                            funcName: '空间预约',
                            funcUrl: '/pages/spaceOrder1/spaceOrder1',
                            functionPic: '/static/packageMy/search/img/icon1-4.png'
                        },
                        {
                            funcName: '活动报名',
                            funcUrl: '/pages/activeOrder1/activeOrder1',
                            functionPic: '/static/packageMy/search/img/icon1-5.png'
                        },
                        {
                            funcName: '家头条',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-6.png'
                        },
                        {
                            funcName: '邻里帮',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-7.png'
                        },
                        {
                            funcName: '文e家',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon1-8.png'
                        }
                    ]
                },
                {
                    name: '社区服务',
                    type: 7,
                    funtions: [
                        {
                            funcName: '一键送餐',
                            funcUrl: '/pages/businessOrderMeal1/businessOrderMeal1',
                            functionPic: '../../static/assets/img/home/module/yijiansongcan1.png'
                        },
                        {
                            funcName: '享优待',
                            funcUrl: '',
                            functionPic: '../../static/assets/img/home/module/xiangyoudai.png'
                        },
                        {
                            funcName: '约服务',
                            funcUrl: '',
                            functionPic: '../../static/assets/img/home/module/yuefuwu.png'
                        },
                        {
                            funcName: '智守护',
                            funcUrl: '',
                            functionPic: '../../static/assets/img/home/module/zhishouhu.png'
                        },
                        {
                            funcName: '国际交流',
                            funcUrl: '/packageHome/international/international',
                            functionPic: '/static/packageMy/search/img/icon2-5.png'
                        },
                        {
                            funcName: '特殊人群通道',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon2-6.png'
                        },
                        {
                            funcName: '物业缴费',
                            funcUrl: '/payment/pages/list/list',
                            functionPic: '/static/packageMy/search/img/icon2-7.png'
                        },
                        {
                            funcName: '房屋出租登记',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon2-8.png'
                        },
                        {
                            funcName: '流动人口登记',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon2-9.png'
                        },
                        {
                            funcName: '老人关怀',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon2-10.png'
                        }
                    ]
                },
                {
                    name: '交通出行',
                    type: 7,
                    funtions: [
                        {
                            funcName: '邻里停',
                            urlType: 3,
                            funcUrl: 'wx9f3793476d539c88',
                            functionPic: '/static/packageMy/search/img/icon3-1.png'
                        },
                        {
                            funcName: '社区微公交',
                            funcUrl: '',
                            functionPic: '../../static/assets/img/home/module/weigongjiao.png'
                        },
                        {
                            funcName: '社区拼车',
                            funcUrl: '/pages/communityCar1/communityCar1',
                            functionPic: '../../static/assets/img/home/module/shequpinche.png'
                        }
                    ]
                },
                {
                    name: '数字治理',
                    type: 7,
                    funtions: [
                        // {"funcName": "组织架构","funcUrl": "","functionPic":"./img/icon4-1.png"},
                        // {"funcName": "组织生活","funcUrl": "","functionPic":"./img/icon4-2.png"},
                        // {"funcName": "在线学习","funcUrl": "","functionPic":"./img/icon4-3.png"},
                        // {"funcName": "百年党史","funcUrl": "","functionPic":"./img/icon4-4.png"},
                        // {"funcName": "网格在线","funcUrl": "","functionPic":"./img/icon4-5.png"},
                        // {"funcName": "民主自治","funcUrl": "","functionPic":"./img/icon4-6.png"},
                        {
                            funcName: '微心愿',
                            funcUrl: '/packageHome/wish/wish',
                            functionPic: '/static/packageMy/search/img/icon4-1.png'
                        },
                        {
                            funcName: '党员亮相',
                            funcUrl: '/squareList/pages/list2/list2',
                            functionPic: '/static/packageMy/search/img/icon4-2.png'
                        },
                        {
                            funcName: '党员学习',
                            funcUrl: '/onlineLearning/pages/list/list',
                            functionPic: '/static/packageMy/search/img/icon4-3.png'
                        },
                        {
                            funcName: '党代表工作室',
                            funcUrl: '/packageHome/studio/studio?type=1',
                            functionPic: '/static/packageMy/search/img/icon4-4.png'
                        },
                        {
                            funcName: '民主议事',
                            funcUrl: '/packageHome/official/official',
                            functionPic: '/static/packageMy/search/img/icon4-5.png'
                        },
                        {
                            funcName: '报事报修',
                            funcUrl: '/report/pages/main/main',
                            functionPic: '/static/packageMy/search/img/icon4-6.png'
                        },
                        {
                            funcName: '建议反馈',
                            funcUrl: '/packageHome/suggestion/suggestion',
                            functionPic: '/static/packageMy/search/img/icon4-7.png'
                        }
                    ]
                },
                {
                    name: '文化教育',
                    type: 7,
                    funtions: [
                        {
                            funcName: '普惠托育',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon5-1.png'
                        },
                        {
                            funcName: '幸福学堂',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon5-2.png'
                        },
                        {
                            funcName: '社区达人',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon5-3.png'
                        },
                        {
                            funcName: '精选课程',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon5-4.png'
                        },
                        {
                            funcName: '教育活动',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon5-5.png'
                        }
                    ]
                },
                {
                    name: '居民健康',
                    type: 7,
                    funtions: [
                        {
                            funcName: '健身打卡',
                            funcUrl: '',
                            functionPic: '/static/packageMy/search/img/icon6-1.png'
                        }
                    ]
                }
            ],

            allList: [],
            serchList: [],
            searchName: '',

            j: {
                funcUrl: '',
                urlType: '',
                id: '',
                funcName: ''
            }
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        // this.queryAllFuncitonTree()
        if (uni.getStorageSync('role_type') == 1) {
            this.getMenuByUser();
        } else {
            this.getMenuByStaff();
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    // onShareAppMessage: function () {
    // }
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */ methods: {
        onChange(e) {
            this.setData({
                searchName: e.detail
            });
            this.findData(this.searchName);
        },

        goLink(url) {
            if (url.indexOf('linlibang') > -1) {
                this.gotoLlb();
            } else {
                uni.navigateTo({
                    url: '/pages/link/link?url=' + encodeURIComponent(url)
                });
            }
        },

        goTip() {
            uni.showToast({
                title: '功能开发中，敬请期待',
                icon: 'none'
            });
        },

        goPage(event) {
            let url = event.currentTarget.dataset.url;
            let urlType = event.currentTarget.dataset.type;

            if (url && url != 'null') {
                if (urlType == 3) {
                    //跳转小程序
                    uni.navigateToMiniProgram({
                        appId: url,
                        shortLink: url,

                        success(res) {
                            // 打开成功
                        }
                    });
                } else if (urlType == 1) {
                    this.goLink(url);
                } else {
                    uni.navigateTo({
                        url
                    });
                }
            } else {
                this.goTip();
            }
        },

        onSearch() {
            // wx.showToast({ title: '搜索' + this.data.searchName, icon: "none" });
            // this.queryAllFuncitonTree()
            this.findData(this.searchName);
        },

        findData(key) {
            // console.log(key)
            if (key.trim() == '') {
                this.setData({
                    serchList: []
                });
                return false;
            }

            let serchList = [];
            this.allList.map((item) => {
                item.funtions.map((j) => {
                    if (j.funcName.indexOf(key) != -1) {
                        serchList.push(j);
                    }
                });
            }); // console.log(serchList)

            this.setData({
                serchList
            });
        },

        queryAllFuncitonTree() {
            uni.$post(`${queryAllFuncitonTree}?funcType=5&seach=${this.searchName}`)
                .then((res) => {
                    //请求成功
                    if (res.code == 0) {
                        this.setData({
                            allList: res.data
                        });
                    } else {
                        this.setData({
                            allList: []
                        });
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });
                    }
                })
                .catch((err) => {
                    //请求失败
                });
        },

        //业主菜单
        getMenuByUser() {
            let authState = uni.getStorageSync('authState');
            let vister = authState == 3 ? false : true; //scope 1-全部，2-首页

            uni.$post(`${getMenuByUser}?projectUnionId=${uni.getStorageSync('projectUnionId')}&vister=${vister}&scope=1`)
                .then((res) => {
                    //请求成功
                    if (res.code == 0) {
                        this.setData({
                            allList: res.data
                        });
                    } else {
                        uni.showToast({
                            title: res.message,
                            icon: 'none'
                        });
                        this.setData({
                            allList: []
                        });
                    }
                })
                .catch((err) => {
                    //请求失败
                    this.setData({
                        allList: []
                    });
                });
        },

        //员工菜单
        getMenuByStaff() {
            let authState = uni.getStorageSync('authState');
            let vister = authState == 3 ? false : true;
            uni.$post(
                getMenuByStaff +
                    '?staffUnionId=' +
                    uni.getStorageSync('staffUnionId') +
                    '&organizeUnionId=' +
                    uni.getStorageSync('organizeUnionId') +
                    '&type=' +
                    uni.getStorageSync('organizeType') +
                    '&scope=2' +
                    '&vister=' +
                    vister
            )
                .then((res) => {
                    //请求成功
                    if (res.code == 0) {
                        this.setData({
                            allList: res.data
                        });
                    } else {
                        uni.showToast({
                            title: res.message,
                            icon: 'none'
                        });
                        this.setData({
                            allList: []
                        });
                    }
                })
                .catch((err) => {
                    //请求失败
                    this.setData({
                        allList: []
                    });
                });
        },

        /***
         * 跳转第三方h5应用处理
         */

        /***邻里帮 */
        //邻里帮第一步-用户信息同步
        gotoLlb() {
            let phone = uni.getStorageSync('phoneNumber'); // let phone = '15068748665'

            uni.$get(api.userSyncllb + '?mobile=' + phone + '&appId=' + app.globalData.appId).then((res) => {
                if (res.success) {
                    this.gotoLoginllb();
                } else if (res.busiCode == 'userMsgNoComplete') {
                    //跳转到完善信息页面
                    uni.navigateTo({
                        url: '/pages/ownerCertification/ownerCertification'
                    });
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });
        },

        //邻里帮-第二步用户登录接口-返回url
        gotoLoginllb() {
            let phone = uni.getStorageSync('phoneNumber'); // let phone = '15068748665'

            uni.$get(api.userLoginllb + '?mobile=' + phone + '&appId=' + app.globalData.appId).then((res) => {
                if (res.success) {
                    uni.navigateTo({
                        url: '/pages/link/link?url=' + res.data
                    });
                } else {
                    uni.showToast({
                        title: res.msg || '跳转异常',
                        icon: 'none'
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './search.css';
</style>
